--To close all the database connections.
USE master
 
GO
ALTER DATABASE DatabaseName
SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE DatabaseName
SET ONLINE
GO